﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="../../Styles/Site.css" media="screen" />
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <link rel="stylesheet" href="../../Styles/qunit-git.css" media="screen" />
    <script type="text/javascript" src="../../scripts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="../../Scripts/qunit-git.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.columnsresize.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.grouping.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.pager.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.sort.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.filter.js"></script> 
    <script type="text/javascript">
        $(document).ready(function () {
            // Test Grid Selection functionality.

            // prepare the data
            var data = new Array();

            var firstNames =
            [
                "Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"
            ];

            var lastNames =
            [
                "Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"
            ];

            var productNames =
            [
                "Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"
            ];

            var priceValues =
            [
                "2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"
            ];

            var generaterow = function (index) {
                if (index == undefined) index = "id";

                var row = {};
                var productindex = Math.floor(Math.random() * productNames.length);
                var price = parseFloat(priceValues[productindex]);
                var quantity = 1 + Math.round(Math.random() * 10);
                row["id"] = index;
                row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
                row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
                row["productname"] = productNames[productindex];
                row["price"] = price;
                row["quantity"] = quantity;
                row["total"] = price * quantity;
                return row;
            }

            for (var i = 0; i < 24; i++) {
                data[i] = generaterow(i);
            }

            var source =
            {
                localdata: data,
                datatype: "array",
                id: 'id'
            };

            $("#jqxgrid").jqxGrid(
            {
                width: 400,
                height: 100,
                source: source,
                columns: [
                  { text: 'First Name', datafield: 'firstname', width: 100 },
                  { text: 'Last Name', datafield: 'lastname', width: 100 },
                  { text: 'Product', datafield: 'productname', width: 180 },
                  { text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right' },
                  { text: 'Unit Price', datafield: 'price', width: 90, cellsalign: 'right', cellsformat: 'c2' },
                  { text: 'Total', datafield: 'total', width: 100, cellsalign: 'right', cellsformat: 'c2' }
                ]
            });

            module("Module Selection");

            test("selectrow function test", function () {
                $('#jqxgrid').jqxGrid('clearselection');
                $('#jqxgrid').jqxGrid('selectrow', 0);
                var selectedrow = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(0 == selectedrow, "selectrow test.");
            });

            test("unselectrow function test", function () {
                $('#jqxgrid').jqxGrid('clearselection');
                $('#jqxgrid').jqxGrid('selectrow', 0);
                $('#jqxgrid').jqxGrid('unselectrow', 0);
                var selectedrow = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(-1 == selectedrow, "unselectrow test.");
            });

            test("multipleselection test", function () {
                $('#jqxgrid').jqxGrid('clearselection');
                $('#jqxgrid').jqxGrid('selectionmode', 'multiplerows');
                $('#jqxgrid').jqxGrid('selectrow', 0);
                $('#jqxgrid').jqxGrid('selectrow', 1);
                $('#jqxgrid').jqxGrid('selectrow', 2);

                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 3, "multiple selection test.");
                ok(selectedindexes[0] == 0, "first row selection test.");
                ok(selectedindexes[1] == 1, "second row selection test.");
                ok(selectedindexes[2] == 2, "third row selection test.");

                $('#jqxgrid').jqxGrid('unselectrow', 2);
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 2, "multiple selection test after 1 unselectrow function call.");
                ok(selectedindexes[0] == 0, "first row selection test after unselectrow function call.");
                ok(selectedindexes[1] == 1, "second row selection test after unselectrow function call.");
                ok(selectedindexes[2] == undefined, "third row selection test after unselectrow function call.");

                $('#jqxgrid').jqxGrid('unselectrow', 5);
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 2, "multiple selection test after 2 unselectrow call with invalid index.");

                $('#jqxgrid').jqxGrid('unselectrow', 1);
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 1, "multiple selection test after 2 unselectrow function call.");
                ok(selectedindexes[0] == 0, "first row selection test after 2 unselectrow function call.");
                ok(selectedindexes[1] == undefined, "second row selection test after 2 unselectrow function call.");
                ok(selectedindexes[2] == undefined, "third row selection test after 2 unselectrow function call.");

                $('#jqxgrid').jqxGrid('unselectrow', 0);
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 0, "multiple selection test after 3 unselectrow function call.");
                ok(selectedindexes[0] == undefined, "first row selection test after 3 unselectrow function call.");
                ok(selectedindexes[1] == undefined, "second row selection test after 3 unselectrow function call.");
                ok(selectedindexes[2] == undefined, "third row selection test after 3 unselectrow function call.");
            });

            test("selection modes tests", function () {
                $('#jqxgrid').jqxGrid('clearselection');
                $('#jqxgrid').jqxGrid('selectionmode', 'singlerow');
                $('#jqxgrid').jqxGrid('selectrow', 0);
                $('#jqxgrid').jqxGrid('selectrow', 1);
                $('#jqxgrid').jqxGrid('selectrow', 2);

                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 1, "single row test.");
                ok(selectedindexes[0] == 2, "single row test.");
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == 2, "selectedindex test.");
                $('#jqxgrid').jqxGrid('unselectrow', 2);
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == -1, "selectedindex test after unselectrow call.");
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 0, "selectedindexes test after unselectrow call.");

                $('#jqxgrid').jqxGrid('clearselection');
                $('#jqxgrid').jqxGrid('selectionmode', 'multiplerows');
                $('#jqxgrid').jqxGrid('selectrow', 0);
                $('#jqxgrid').jqxGrid('selectrow', 1);
                $('#jqxgrid').jqxGrid('selectrow', 2);

                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 3, "selectionmode multiplerows test.");
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == 2, "selectionmode multiplerows.");
                // toggle the selection
                $('#jqxgrid').jqxGrid('selectrow', 2);
                ok(selectedindexes.length == 2, "toggle selection test.");
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == 1, "selectedindex after toggle selection test.");

                $('#jqxgrid').jqxGrid('selectrow', 1);
                ok(selectedindexes.length == 1, "toggle selection test. Unselect second row.");
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == 0, "selectedindex after toggle selection test. Unselect second row.");

                $('#jqxgrid').jqxGrid('selectrow', 0);
                ok(selectedindexes.length == 0, "toggle selection test. Unselect first row.");
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == -1, "selectedindex after toggle selection test. Unselect first row.");


                $('#jqxgrid').jqxGrid('clearselection');
                $('#jqxgrid').jqxGrid('selectionmode', 'none');
                $('#jqxgrid').jqxGrid('selectrow', 0);
                $('#jqxgrid').jqxGrid('selectrow', 1);
                $('#jqxgrid').jqxGrid('selectrow', 2);
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 3, "selectionmode = none test.");
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == 2, "selectionmode = none selectedindex test.");

                $('#jqxgrid').jqxGrid('selectrow', 0);
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 3, "selectionmode = none test after selectrow call.");
                var selectedindex = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(selectedindex == 0, "selectionmode = none selectedindex test.");
                $('#jqxgrid').jqxGrid('clearselection');
                var selectedindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
                ok(selectedindexes.length == 0, "selectionmode = none test after clearselection call.");
            });

            test("clearselection function test", function () {
                $('#jqxgrid').jqxGrid('selectrow', 0);
                $('#jqxgrid').jqxGrid('clearselection');
                var selectedrow = $('#jqxgrid').jqxGrid('getselectedrowindex');
                ok(-1 == selectedrow, "clearselection test.");
            });

            test("rowselect test", function () {
                $('#jqxgrid').jqxGrid('clearselection');
                var selectrow = false;
                $("#jqxgrid").bind('rowselect', function () {
                    selectrow = true;
                });

                $('#jqxgrid').jqxGrid('selectrow', 0);
                ok(selectrow == true, "rowselect test");

                $("#jqxgrid").unbind('rowselect');
            });

            test("rowunselect test", function () {
                $('#jqxgrid').jqxGrid('clearselection');
                var selectrow = false;
                $("#jqxgrid").bind('rowunselect', function () {
                    selectrow = true;
                });

                $('#jqxgrid').jqxGrid('selectrow', 0);
                $('#jqxgrid').jqxGrid('unselectrow', 0);
                ok(selectrow == true, "rowunselect test");

                $("#jqxgrid").unbind('rowunselect');
            });
        });
    </script>
</head>
<body>
    <div>
        <h1 id="qunit-header">
            QUnit example</h1>
        <h2 id="qunit-banner">
        </h2>
        <div id="qunit-testrunner-toolbar">
        </div>
        <h2 id="qunit-userAgent">
        </h2>
        <ol id="qunit-tests">
        </ol>
        <div id="qunit-fixture">
            test markup, will be hidden</div>
    </div>
    <div style="display: none;" id="jqxgrid">
    </div>
    <div style="display: none;" id="jqxgrid2">
    </div>
</body>
</html>
